package ui.panels.primary.first;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import ui.flowManager.FlowManagerImpl;
import ui.panels.commands.NextCommand;
import ui.panels.commands.OpenXMLExperimentCommand;

/**
 * @author vladimir
 *  ActionListener for:
 *  	1. Button Manual
 *  	2. Button From File
 *
 */
public class ActionList implements ActionListener{
	private FirstPanel firstPanel = null;
	
	public ActionList(FirstPanel panel)
	{
		this.firstPanel = panel;
	}
	
	public void actionPerformed(ActionEvent event) {
		
		//case manual
		if(event.getSource() == firstPanel.getManual())
		{	
			//open the next panel
			NextCommand nextCommand = new NextCommand();
			nextCommand.execute();
		}
		
		//case from file
		else if(event.getSource() == firstPanel.getFromFile())
		{
			JFileChooser fileChooser = new JFileChooser();
			//open the window for files
			int choice = fileChooser.showOpenDialog(null);
			
			//case file waas opened
			if(choice == JFileChooser.APPROVE_OPTION)
			{
				//take the file
				File file = fileChooser.getSelectedFile();
				//create, set and rum the open XML command
				OpenXMLExperimentCommand openXMLExperimentCommand = new OpenXMLExperimentCommand();
				openXMLExperimentCommand.setFile(file);
				
				openXMLExperimentCommand.execute();
				
				//jump to third panel
				FlowManagerImpl.getInstance().createNextPanel();
				FlowManagerImpl.getInstance().createNextPanel();
				
			}
		}	
	}
}
